package com.ssplayer.custom;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.nineoldandroids.view.ViewHelper;
import com.ssplayer.activity.R;
import com.ssplayer.utils.ScreenUtils;

public class SlidingMenu extends HorizontalScrollView {
	/**
	 * 屏幕宽度
	 */
	private int mScreenWidth;
	private int mMenuLeftPadding;
	/**
	 * 菜单的宽度
	 */
	private int mMenuWidth;
	private int mHalfMenuWidth;

	public boolean isOpen;

	private boolean once;

	private ViewGroup mMenu;
	private ViewGroup mContent;

	public SlidingMenu(Context context, AttributeSet attrs) {
		this(context, attrs, 0);

	}

	public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mScreenWidth = ScreenUtils.getScreenWidth(context);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
				R.styleable.SlidingMenu, defStyle, 0);

		int n = a.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = a.getIndex(i);
			switch (attr) {
			case R.styleable.SlidingMenu_leftPadding:
				// 默认50
				mMenuLeftPadding = a.getDimensionPixelSize(attr,
						(int) TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 50f,
								getResources().getDisplayMetrics()));// 默认为10DP
				break;
			}
		}
		a.recycle();
	}

	public SlidingMenu(Context context) {
		this(context, null, 0);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		/**
		 * 显示的设置一个宽度
		 */
		if (!once) {
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			mContent = (ViewGroup) wrapper.getChildAt(0);
			mMenu = (ViewGroup) wrapper.getChildAt(1);

			// 计算菜单的宽度
			mMenuWidth = mScreenWidth - mMenuLeftPadding;
			mHalfMenuWidth = mMenuWidth / 2;
			// mScreenWidth=720
			// mMenuLeftPadding=200
			// mMenuWidth=520
			// 设置页面和菜单的宽度
			mContent.getLayoutParams().width = mScreenWidth;
			mMenu.getLayoutParams().width = mMenuWidth;

		}
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (changed) {
			// 将菜单隐藏
			this.scrollTo(0, 0);
			once = true;
		}
	}

	@SuppressLint("ClickableViewAccessibility")
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// 直接返回true，禁用手动滑动
		return true;
		// int action = ev.getAction();
		// switch (action) {
		// // Up时，进行判断，如果显示区域大于菜单宽度一半则完全显示，否则隐藏
		// case MotionEvent.ACTION_UP:
		// int scrollX = getScrollX();
		// if (scrollX > mHalfMenuWidth) {
		// // 显示菜单
		// openMenu();
		// } else if (scrollX < mHalfMenuWidth) {
		// // 隐藏菜单
		// closeMenu();
		// }
		// return true;
		// }
		// return super.onTouchEvent(ev);
	}

	/**
	 * 打开菜单
	 */
	public void openMenu() {
		this.smoothScrollTo(mMenuWidth, 0);
		isOpen = true;
	}

	/**
	 * 关闭菜单
	 */
	public void closeMenu() {
		this.smoothScrollTo(0, 0);
		isOpen = false;
	}

	/**
	 * 切换菜单状态
	 */
	public void toggle() {
		if (isOpen) {
			closeMenu();
		} else {
			openMenu();
		}
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		// l:(520--0)
		// scale缩放比例(1--0)
		float scale = l * 1.0f / mMenuWidth;
		float leftScale = 1 - 0.2f * scale;
		float rightScale = 0.8f + scale * 0.2f;

		ViewHelper.setPivotX(mContent, mScreenWidth);
		ViewHelper.setPivotY(mContent, mContent.getHeight() / 2);
		ViewHelper.setScaleX(mContent, leftScale);
		ViewHelper.setScaleY(mContent, leftScale);

		ViewHelper.setScaleX(mMenu, rightScale);
		ViewHelper.setScaleY(mMenu, rightScale);
		ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (scale));
		// 根据滚动位置计算菜单的平移的距离，使其平移时速度显得快一点
		ViewHelper.setTranslationX(mMenu, mMenuWidth * (1 - scale) * 0.6f);
	}

}
